package com.onion.auth;

import com.onion.auth.support.AuthManager;
import com.onion.auth.support.AuthorizeProvider;
import com.onion.auth.support.provider.CacheAuthProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;

@Configuration
public class OnionAuthStarter {

    @Autowired
    private Environment environment;

    @Bean("authManager")
    @DependsOn("authorizeProvider")
    public AuthManager getAuthManager(AuthorizeProvider<String> authorizeProvider){
        AuthManager authManager = new AuthManager();
        authManager.setAuthorizeProvider(authorizeProvider);
        return authManager;
    }

    @Bean("authorizeProvider")
    public AuthorizeProvider<String> getAuthorizeProvider(){
        String authorizeProvider = environment.getProperty("onion.auth.provider");
        if(StringUtils.isBlank(authorizeProvider)){
            return new CacheAuthProvider();
        }
        throw new RuntimeException("无效的AuthorizeProvider");
    }

}